package com.example.zhou.musicplayer;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;

public class SongList extends AppCompatActivity {
    private LinkedList<String> musicNames=new LinkedList<String>();

     class MusicNameAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
         private final LinkedList<String> musicNames;

         MusicNameAdapter(final LinkedList<String> musicNames) {
             this.musicNames=musicNames;
        }
        class ViewHolder extends RecyclerView.ViewHolder{
            View view;
            TextView textView;
            public ViewHolder(View view){
                super(view);
                this.view=view;
                textView=(TextView)view.findViewById(R.id.musicName);
            }
        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.music_item,parent,false);
            return new ViewHolder(view);
        }
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position){
            final ViewHolder viewHolder=(ViewHolder) holder;
            viewHolder.textView.setText(musicNames.get(position));
            viewHolder.view.setOnClickListener(new View.OnClickListener(){
                private int position=viewHolder.getAdapterPosition();
                @Override
                public void onClick(View view) {
                    String selectedMusicName=musicNames.get(position);
                    Intent intent=new Intent();
                    intent.putExtra("selectedMusicName",selectedMusicName);
                    setResult(RESULT_OK,intent);
                    finish();
                }
            });
        }
        @Override
        public int getItemCount() {
            return musicNames.size();
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_song_list);
        String[] files=Environment.getExternalStorageDirectory().list();
        for(int i=0;i<files.length;++i)
        {
            if((Pattern.compile(".*\\.(mp3|ogg)")).matcher(files[i]).matches()){
                musicNames.add(files[i]);
            }
        }
        RecyclerView recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new MusicNameAdapter(musicNames));
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
    }
}
